package gui.edit_objects;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import objects.Telephone;

import bdd_handler.TelephoneDAO;
import bdd_handler.bdd;


public class Edit_Telephone extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;

	private JFrame parent;

	private TelephoneDAO tDAO;
	private Telephone t;
	private JTextField telephone_nom, numero;
	private boolean mknew;
	private JButton valider, annuler;

	public Edit_Telephone(JFrame parent, Telephone telephone){
		//recuperation de la fenetre parente
		this.parent=parent;
		this.parent.setVisible(false);

		//construction de la fenetre
		this.t=telephone;
		if(t.getId_telephone()==-1)
			mknew=true;
		else
			mknew=false;
		if(mknew)
			this.setTitle("Nouveau Téléphone");
		else
			this.setTitle("Editer Téléphone");
		this.setSize(500,200);
		this.setResizable(false);
		JPanel pan=new JPanel();
		pan.setLayout(null);
		this.setContentPane(pan);

		//creation des DAOs
		bdd db=new bdd();
		tDAO=new TelephoneDAO(db.getConnection());

		//creation objets internes
		telephone_nom=new JTextField(t.getType_tel());
		numero=new JTextField(t.getNumero());
		valider=new JButton("valider");
		valider.addActionListener(this);
		annuler=new JButton("annuler");
		annuler.addActionListener(this);
		JLabel lbl_telephone=new JLabel("intitulé du téléphone");
		JLabel lbl_numero=new JLabel("Numéro");

		//placer les objets
		pan.add(lbl_telephone);
		lbl_telephone.setBounds(10,10,200,20);
		pan.add(telephone_nom);
		telephone_nom.setBounds(220, 10, 200, 20);
		pan.add(lbl_numero);
		lbl_numero.setBounds(10, 40, 200, 20);
		pan.add(numero);
		numero.setBounds(220, 40, 200, 20);
		pan.add(annuler);
		annuler.setBounds(10, 70, 90, 20);
		pan.add(valider);
		valider.setBounds(110, 70, 90, 20);

		//action fermeture fenetre
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						close();

					}
				}
				);
		//afficher la fenetre
		this.setVisible(true);
	}

	private void close(){
		this.parent.setVisible(true);
		this.dispose();
	}

	private void send()
	{
		t.setType_tel(telephone_nom.getText());
		t.setNumero(numero.getText());
		if(mknew)
			tDAO.create(t);
		else
			tDAO.update(t);
	}

	private boolean check()
	{
		if(telephone_nom.getText().equals(""))
		{
			JOptionPane.showMessageDialog(this.getContentPane(), "l'intitulé du téléphone n'a pas été renseigné", "Oh zut!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if(numero.getText().equals(""))
		{
			JOptionPane.showMessageDialog(this.getContentPane(), "le numéro de téléphone n'a pas été renseigné", "Oh zut!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		try
		{
			Integer.parseInt(numero.getText());
		}
		catch(NumberFormatException e) {
			JOptionPane.showMessageDialog(this.getContentPane(), "le numéro de téléphone doit être composé de chiffres", "Oh zut!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source=e.getSource();

		if(source==annuler)
		{
			close();
		}
		if(source==valider)
		{
			if(check())
			{
				send();
				close();
			}
		}

	}

}
